package com.chain.BrotherExchange.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.transition.TransitionManager;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.event.SubmitRealNameAuthenticationEvent;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.ui.dialog.SendVerificationCodeDialog;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.TimeCount;
import com.chain.BrotherExchange.viewmodel.IDAuthenticationViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;

/**
 * 实名认证
 */
public class RealNameAuthenticationActivity extends BaseActivity {

    @Bind(R.id.id_edit) EditText mIDEdit;
    @Bind(R.id.name_edit) EditText mNameEdit;
    @Bind(R.id.root_layout) ViewGroup mRootLayout;
    @Bind(R.id.affirm_button) Button mAffirmButton;
    @Bind(R.id.authentication_message_text) TextView mAuthenticationMessageText;

    private IDAuthenticationViewModel mViewModel;

    public static void launch(Context context) {
        context.startActivity(new Intent(context, RealNameAuthenticationActivity.class));
    }

    @Override
    protected int getLayoutID() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        return R.layout.activity_real_name_authentication;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.title_real_name_authentication);

        mViewModel = ViewModelProviders.of(this).get(IDAuthenticationViewModel.class);

        // 认证次数
        initAuthenticationNumber();

        // 认证
        mAffirmButton.setOnClickListener(v ->
                SendVerificationCodeDialog.show(
                        this, view -> sendMessage((TextView) view),
                        this::realNameAuthentication
                ));
    }

    private void realNameAuthentication(SendVerificationCodeDialog dialog) {
        String name = mNameEdit.getText().toString();
        String id = mIDEdit.getText().toString();
        mViewModel.realNameAuthentication(name, id, dialog.getCode()).observe(this, resource ->
                Resource.load(this, resource, dialog, result -> {
                    dialog.successNetwork();
                    EventBus.getDefault().post(new SubmitRealNameAuthenticationEvent());
                    finish();
                })
        );
    }

    private void sendMessage(TextView textView) {
        mViewModel.sendMessage().observe(this, resource -> {
            if (resource.state == Resource.Status.LOADING) {
                textView.setClickable(false);
                textView.setBackgroundColor(Color.parseColor("#CCCCCC"));
                textView.setText(R.string.message_sending);
            } else {
                Androids.makeText(resource.message);
                if (resource.state == Resource.Status.SUCCESS) {
                    new TimeCount(textView).start();
                }
            }
        });
    }

    @SuppressLint("StringFormatInvalid")
    private void initAuthenticationNumber() {
        mViewModel.authenticationNumber().observe(this, resource -> {
            if (resource.state == Resource.Status.SUCCESS) {
                TransitionManager.beginDelayedTransition(mRootLayout);
                mAuthenticationMessageText.setText(
                        String.format(getString(R.string.message_authentication),
                                resource.source.getFirst(), resource.source.getSecond()));
            }
        });
    }

}
